package com.jmhy.sdk.utils;

import com.jmhy.sdk.activity.JmCommunityActivity;
import com.jmhy.sdk.activity.JmUserinfoActivity;
import com.jmhy.sdk.common.JiMiSDK;
import com.jmhy.sdk.config.AppConfig;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import java.net.URISyntaxException;
import java.util.Timer;
import java.util.TimerTask;


public class JsInterface {
    private Activity activity;

    private Seference mSeference;

    public JsInterface(Activity activity) {
        this.activity = activity;
        mSeference = new Seference(activity);
    }

    @JavascriptInterface
    public void JavaScriptSavepassword(String user, String pwd, String uid) {
        mSeference.saveAccount(user, pwd, uid);
        AppConfig.saveMap(user, pwd, uid);
        Utils.saveUserToSd(activity);
    }

    @JavascriptInterface
    public void JavaScriptToJumpLogin() {
        if (activity != null) {
            activity.finish();
        }
    }

    @JavascriptInterface
    public void closeWebView() {
        if (activity != null) {
            activity.finish();
        }
    }

    @JavascriptInterface
    public void JavaScriptback() {
        Logger.d("JavaScriptback");
        if (activity != null) {
            activity.finish();
        }
    }

    /**
     * 下载链接
     *
     * @param url
     */
    @JavascriptInterface
    public void JavascriptToDown(String url) {

        Intent viewIntent = new Intent("android.intent.action.VIEW",
                Uri.parse(url));
        activity.startActivity(viewIntent);
    }

    @JavascriptInterface
    public void JavaScriptToJumppassword() {
		/*Intent intent = new Intent();
		intent.putExtra("url", AppConfig.USERURL);
		intent.setClass(mContext,  ZYUserinfoActivity.class);
		mContext.startActivity(intent);*/
    }

    @JavascriptInterface
    public void JavaScriptModifyPassword(String pwd) {
        String temUser = mSeference.getPreferenceData(Seference.ACCOUNT_FILE_NAME, Seference.ACCOUNT_1);
        String temUid = mSeference.getPreferenceData(Seference.ACCOUNT_FILE_NAME, Seference.UID_1);
        mSeference.saveAccount(temUser, pwd, temUid);
        AppConfig.saveMap(temUser, pwd, temUid);
        Utils.saveUserToSd(activity);
        Log.i("JavaSsword", pwd);
    }

    @JavascriptInterface
    public void JavaScriptGuestBindOnAccount(String account, String password) {

        String temUid = mSeference.getPreferenceData(Seference.ACCOUNT_FILE_NAME, Seference.UID_1);
        mSeference.saveAccount(account, password, temUid);
        AppConfig.saveMap(account, password, temUid);
        Utils.saveUserToSd(activity);
        Log.i("account and password,", account + "--- password" + password);
    }

    /**
     * 切换账号
     */
    @JavascriptInterface
    public void JavascriptSwitchUser() {
        Log.i("kk", "切换");
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                JiMiSDK.userlistenerinfo.onLogout("logout");
                JiMiSDK.switchAccount(activity);
                activity.finish();
                FloatUtils.destroyFloat();
            }
        });

    }

    /**
     * 跳转浏览器
     */
    @JavascriptInterface
    public void loadUrlByBrowser(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
    }

    @JavascriptInterface
    public void payNotify(String result) {
        //result=0 失败，1 成功
    }

    @JavascriptInterface
    public void closeAnimation() {
        Log.i("JsInterface", "closeAnimation");
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (activity instanceof JmUserinfoActivity) {
                    JmUserinfoActivity userinfoActivity = (JmUserinfoActivity) activity;
                    userinfoActivity.hiddenLoading();
                } else if (activity instanceof JmCommunityActivity) {
                    JmCommunityActivity communityActivity = (JmCommunityActivity) activity;
                    communityActivity.hiddenLoading();
                }
            }
        });
    }


    @JavascriptInterface
    public void forceLogout(final String str) {
        Log.i("JsInterface", "forceLogout");
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                JiMiSDK.forceLogout(str);

            }
        });
    }

    @JavascriptInterface
    public void openWebView(String url, int type) {
        Intent intent = new Intent(activity, JmCommunityActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("type", type);
        activity.startActivity(intent);
    }

	/*@JavascriptInterface
	public void hiddenTip(final String type){
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if(TextUtils.equals("account", type)){
					FloatUtils.hiddenTip(FloatView.INDEX_ACCOUNT);
				}else if(TextUtils.equals("gift", type)){
					FloatUtils.hiddenTip(FloatView.INDEX_GIFT);
				}
			}
		});
	}*/

    //**********************************1.4.3 新增
    @JavascriptInterface
    public void JavascriptCloseLoadingPage() {
        JsInterface.this.closeAnimation();
    }

    @JavascriptInterface
    public void JavascriptSaveQrCode(String qrUrl) {

        MediaUtils.saveImage(this.activity, MediaUtils.getBitmap(qrUrl));

//        DialogUtils.showTip(this.activity, AppConfig.getString(this.activity, "jm_float_snapshot_save"));

        Toast.makeText(activity, AppConfig.getString(this.activity, "jm_float_wx_snapshot_save"), Toast.LENGTH_LONG).show();


        new Timer().schedule(task, 2000);


    }

    private TimerTask task = new TimerTask() {
        @Override
        public void run() {
            openw0x0();
        }
    };

    @JavascriptInterface
    public void openw0x0() {
        try {

            Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
            activity.startActivity(intent);

        } catch (Exception e) {
            e.printStackTrace();

            DialogUtils.showTip(this.activity, AppConfig.getString(this.activity, "jm_no_install_wechat"));
        }
    }

    @JavascriptInterface
    public void openq0q0(String url) {
        Log.i("openq0q0", url);
        if (judgeStrNull(url)) {
            return;
        }
        if (url.equals("mqqwpa://")) {
            try {

                Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq");
                activity.startActivity(intent);

            } catch (Exception e) {
                e.printStackTrace();
                DialogUtils.showTip(this.activity, AppConfig.getString(this.activity, "jm_no_install_qq"));
            }
        } else {
            Intent intent = null;
            try {
                intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                activity.startActivity(intent);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }

        }


    }


    public static boolean judgeStrNull(String str) {
        return (str == null || str.equals(""));
    }

}
